package com.ehl.capture.db;

import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;

public class DictLoader {
    private static Map<String, String> bmdMap = new HashMap();
    private static Map<String, String> hpysMap = new HashMap();
    private static Map<String, String> sbbhMap = new HashMap();
    private static Logger logger = Logger.getLogger(DictLoader.class);

    private Thread dynaLoader = new Thread() {
        public void run() {
            Long lastTime = Long.valueOf(System.currentTimeMillis());
            while (true){
                try{
                    if (System.currentTimeMillis() - lastTime.longValue() > 300000L) {
                        DictLoader.logger.info("tgsInfo is dynamic Updating...");
                        DictLoader.getDictInfo();
                        lastTime = Long.valueOf(System.currentTimeMillis());
                    }

                    Thread.sleep(500L); } catch (Exception e) {
                    while (true) {
                        e.printStackTrace();
                        try {
                            Thread.sleep(5000L);
                        }
                        catch (InterruptedException localInterruptedException)
                        {
                        }
                    }
                }
            }
        }
    };

    public DictLoader(){
        try{
            init();
            logger.info("字典信息初始化完毕");
        } catch (Exception e) {
            logger.error("字典信息初始化失败" + e.toString());
        }
    }

    private void init() {
        getDictInfo();
        this.dynaLoader.start();
    }

    public static void getDictInfo(){
        String sql = "select hphm,hpzl from t_itgs_bmdehl where lx='10'";
        Map tmpMap = new HashMap();
        Object[][] result = DBProxy.getMultiResult(sql);
        if (result != null) {
            for (Object[] line : result) {
                tmpMap.put(line[0] + "~" + line[1], "1");
            }
            bmdMap = tmpMap;
        }

        String sqlHpys = "select hpzl,hpys from t_itgs_hpzl_hpys ";
        Object tmpHpysMap = new HashMap();
        Object[][] resultHpys = DBProxy.getMultiResult(sqlHpys);
        if (resultHpys != null) {
            for (Object[] line : resultHpys) {
                ((Map)tmpHpysMap).put((String)line[0], (String)line[1]);
            }
            hpysMap = (Map)tmpHpysMap;
        }

        String sqlSbbh = "select sb_cloudid||'',sscj from t_itgs_tgsinfo_detail t where t.sscj is not null";
        Object tmpSbbhMap = new HashMap();
        Object[][] resultSbbh = DBProxy.getMultiResult(sqlSbbh);
        if (resultSbbh != null) {
            for (Object[] line : resultSbbh) {
                ((Map)tmpSbbhMap).put((String)line[0], (String)line[1]);
            }
            sbbhMap = (Map)tmpSbbhMap;
        }
    }

    public static boolean isCarInBmd(String car)
    {
        String str = (String)bmdMap.get(car);

        return str != null;
    }

    public static String convert2Hpys(String hpzl)
    {
        if ((hpzl == null) || ("".equals(hpzl))) {
            return "4";
        }
        if (hpzl.length() == 1) {
            hpzl = "0" + hpzl;
        }
        String hpys = (String)hpysMap.get(hpzl);
        if (hpys == null) {
            hpys = "4";
        }
        return hpys;
    }
    public static String getSbbh(String sb_cloudid) {
        String sbbh = (String)sbbhMap.get(sb_cloudid);
        return sbbh;
    }
}